<?php

/**
 * This is the model class for table "real_estate".
 *
 * The followings are the available columns in table 'real_estate':
 * @property integer $id
 * @property string $title
 * @property string $description
 * @property double $lat
 * @property double $long
 * @property integer $square_meters
 * @property string $country
 * @property string $district
 * @property string $local
 * @property integer $category_id
 * @property integer $activ
 * @property integer $user_id
 * @property string $date_add
 * @property string $date_expire
 * @property integer $amount
 * @property string $img1
 * @property string $img2
 * @property string $img3
 * @property string $img4
 * @property string $img5
 */
class RealEstate extends CActiveRecord {

    public $title_en;
 public $title_de;
 public $description_en;
 public $description_de;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return RealEstate the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'real_estate';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(' title, description, lat, long, square_meters, country, district, local,  activ, user_id, date_add, date_expire, amount', 'required'),
			array(' square_meters, category_id, activ, user_id, amount', 'numerical', 'integerOnly'=>true),
			array('lat, long', 'numerical'),
			array('title, country, district, local, img1, img2, img3, img4, img5', 'length', 'max'=>255),
			array('description', 'length', 'max'=>500),
			array('date_add, date_expire', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, description, lat, long, square_meters, country, district, local, category_id, activ, user_id, date_add, date_expire, amount, img1, img2, img3, img4, img5', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		
                return array(
			'id' => 'ID',
			'title' => Yii::t('models','Titulo'),
                        'lat' =>Yii::t('models', 'Latitude'),
			'long' => Yii::t('models','Longitude'),
			'descripton' => Yii::t('models','Descricao'),
			'square_meters' => Yii::t('models','Área(m2)'),
			'country' => Yii::t('models','País'),
			'district' => Yii::t('models','Distrito'),
			'local' => Yii::t('models','Local'),
			'category_id' => Yii::t('models','Categoria'),
			'activ' => Yii::t('models','Ativo'),
			'user_id' => Yii::t('models','Utilizador'),
			'date_add' => Yii::t('models','Adicionado'),
			'date_expire' => Yii::t('models','Expira'),
			'amount' => Yii::t('models','Preço'),
			'img1' => 'Img1',
			'img2' => 'Img2',
			'img3' => 'Img3',
			'img4' => 'Img4',
			'img5' => 'Img5',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
        
        public function searchFrontEnd() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('activ', 1);
  
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }
	
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $us = new RWebUser();
        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('lat', $this->lat);
        $criteria->compare('long', $this->long);
        $criteria->compare('square_meters', $this->square_meters);
        $criteria->compare('country', $this->country, true);
        $criteria->compare('district', $this->district, true);
        $criteria->compare('local', $this->local, true);
        $criteria->compare('category_id', $this->category_id);
        //$criteria->compare('activ',1);
        $criteria->compare('user_id', !Rights::getAuthorizer()->isSuperuser($us->getId()) === false ? '' : $us->getId());
        $criteria->compare('date_add', $this->date_add, true);
        //$criteria->compare('date_expire', date('Y-m-d'), true);
        $criteria->compare('amount', $this->amount);


        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function behaviors() {
        return array(
            array(
                'class' => 'ext.seo.components.SeoRecordBehavior',
                'route' => 'model/view',
                'params' => array('id' => $this->id, 'name' => $this->title),
            ),
        );
    }

}